<template>
  <div>
    <el-dialog
      :title="form.id ? '编辑轮播图' : '添加轮播图'"
      :visible.sync="isshow"
      @close="reset"
    >
      <el-form :model="form" :rules="rules" ref="bannerForm">
        <el-form-item label="标题" :label-width="formLabelWidth" prop="title">
          <el-input
            clearable
            v-model="form.title"
            autocomplete="off"
          ></el-input>
        </el-form-item>
        <el-form-item
          v-if="form.pid != 0 || !form.id"
          label="图片"
          :label-width="formLabelWidth"
        >
          <el-upload
            :on-preview="preview"
            :on-exceed="exceed"
            :file-list="imgUrl"
            action=""
            :auto-upload="false"
            list-type="picture-card"
            :on-change="getFile"
            :on-remove="removeFile"
            :limit="1"
          >
            <i class="el-icon-plus"></i>
          </el-upload>
          <el-dialog :visible.sync="dialogVisible">
            <img width="100%" :src="dialogImageUrl" alt="" />
          </el-dialog>
        </el-form-item>
        <el-form-item label="状态" :label-width="formLabelWidth">
          <el-switch
            v-model="form.status"
            active-color="#13ce66"
            inactive-color="#ff4949"
            :active-value="1"
            :inactive-value="2"
          >
          </el-switch>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="reset">取 消</el-button>
        <el-button type="primary" @click="submitForm">确 定</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { mapActions } from "vuex";
import { bannerinfo, banneraddedit } from "../../request/api";
export default {
  data() {
    return {
      dialogVisible: false,
      dialogImageUrl: "",
      isshow: false,
      formLabelWidth: "120px",
      imgUrl: [],
      form: {
        title: "",
        img: "",
        status: 2,
      },
      rules: {
        title: [{ required: true, message: "请输入标题", trigger: "blur" }],
      },
    };
  },
  methods: {
    ...mapActions("banner", ["getBannerList"]),
    getFile(file) {
      this.form.img = file.raw;
    },
    removeFile() {
      this.form.img = "";
    },
    exceed() {
      this.$message.error("上传的文件个数超出限制");
    },
    preview(file) {
      this.dialogVisible = true;
      this.dialogImageUrl = file.url;
    },
    reset() {
      this.isshow = false;
      this.dialogVisible = false;
      this.dialogImageUrl = "";
      this.$refs.bannerForm.resetFields();
      this.imgUrl = [];
      this.form = {
        title: "",
        img: "",
        status: 2,
      };
    },
    submitForm() {
      this.$refs.bannerForm.validate((val) => {
        if (!val) {
          return;
        }
        let url = this.form.id ? "/banneredit" : "/banneradd";
        banneraddedit(url, this.form).then((res) => {
          if (res.code == 200) {
            this.$message.success(res.msg);
            this.getBannerList();
            this.reset();
            return;
          }
          this.$message.error(res.msg);
        });
      });
    },
    getInfo(id) {
      this.isshow = true;
      bannerinfo({ id }).then((res) => {
        if (res.code == 200) {
          this.form = res.list;
          this.form.id = id;
          this.imgUrl = res.list.img
            ? [{ url: "http://localhost:3000" + res.list.img }]
            : [];
        }
      });
    },
  },
};
</script>

<style scoped>
</style>